//@author A0096759E

package testcases;


import static org.junit.Assert.assertEquals;
import logic.CommandDecoder;

import org.junit.Test;

import storage.ScheduleEvent;
import storage.Storage;

public class CommandDecoderTestSort {

	Storage storage = new Storage();
	CommandDecoder commandDecoder = new CommandDecoder(storage);
	
	private static final String SUCCESS_ALL_EVENTS_CLEARED = "All events have been cleared from your Schedule!";
	private static final String SUCCESS_ADDED_EVENT = "The event \"%s\" has been added successfully to your Schedule!";
	private static final String SUCCESS_SORTED_BY_ALPHABET = "Your Schedule has successfully been sorted in Alphabetical Order!";
	private static final String SUCCESS_SORTED_BY_TIME = "Your Schedule has successfully been sorted by Time!";
	private static final String SUCCESS_SORTED_BY_TYPE = "Your Schedule has successfully been sorted by Type!";
	
	@Test
	public void test() {
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storage.clearAllEvents());
		StringBuilder correctSequence = new StringBuilder();
		
		ScheduleEvent eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		ScheduleEvent eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		ScheduleEvent eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		ScheduleEvent eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("1. " + eventToAdd1.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd3.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd4.toString());

		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storage.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storage.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storage.addEvent(eventToAdd1));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storage.addEvent(eventToAdd3));
		
		assertEquals(SUCCESS_SORTED_BY_ALPHABET, commandDecoder.decodeUserInput("sort -sa"));
		assertEquals(correctSequence.toString(), storage.toString());
		
		//Sorted By Time
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storage.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("1. " + eventToAdd1.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd3.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd4.toString());

		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storage.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storage.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storage.addEvent(eventToAdd1));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storage.addEvent(eventToAdd3));
		
		assertEquals(SUCCESS_SORTED_BY_TIME, commandDecoder.decodeUserInput("sort"));
		assertEquals(correctSequence.toString(), storage.toString());
		
		//Sorted By Type
		assertEquals(SUCCESS_ALL_EVENTS_CLEARED,storage.clearAllEvents());
		correctSequence = new StringBuilder();
		
		eventToAdd1 = new ScheduleEvent(0, "Apple Juice", "20121991");
		eventToAdd2 = new ScheduleEvent(1, "Banana Milkshake", "21121991", "1200");
		eventToAdd3 = new ScheduleEvent(2, "Carrots Are Nice", "22121991", "1200", "1230");
		eventToAdd4 = new ScheduleEvent(0, "Donkeys are short", "23121991");

		correctSequence.append("1. " + eventToAdd1.toString()+"\n\n");
		correctSequence.append("2. " + eventToAdd4.toString()+"\n\n");
		correctSequence.append("3. " + eventToAdd2.toString()+"\n\n");
		correctSequence.append("4. " + eventToAdd3.toString());
		
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Banana Milkshake" ),storage.addEvent(eventToAdd2));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Apple Juice" ),storage.addEvent(eventToAdd1));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Donkeys are short" ),storage.addEvent(eventToAdd4));
		assertEquals(String.format(SUCCESS_ADDED_EVENT, "Carrots Are Nice" ),storage.addEvent(eventToAdd3));
		
		assertEquals(SUCCESS_SORTED_BY_TYPE, commandDecoder.decodeUserInput("sort -st"));
		assertEquals(correctSequence.toString(), storage.toString());
	}

}
